package cn.csc.module.pay.controller.admin.funddetails;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.pay.controller.admin.funddetails.vo.*;
import cn.csc.module.pay.dal.dataobject.funddetails.PayFundDetailsDO;
import cn.csc.module.pay.convert.funddetails.PayFundDetailsConvert;
import cn.csc.module.pay.service.funddetails.PayFundDetailsService;

@Api(tags = "管理后台 - 用户资金收支明细")
@RestController
@RequestMapping("/pay/fund-details")
@Validated
public class PayFundDetailsController {

    @Resource
    private PayFundDetailsService fundDetailsService;

    @PostMapping("/create")
    @ApiOperation("创建用户资金收支明细")
    @PreAuthorize("@ss.hasPermission('pay:fund-details:create')")
    public CommonResult<BigInteger> createFundDetails(@Valid @RequestBody PayFundDetailsCreateReqVO createReqVO) {
        return success(fundDetailsService.createFundDetails(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户资金收支明细")
    @PreAuthorize("@ss.hasPermission('pay:fund-details:update')")
    public CommonResult<Boolean> updateFundDetails(@Valid @RequestBody PayFundDetailsUpdateReqVO updateReqVO) {
        fundDetailsService.updateFundDetails(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户资金收支明细")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details:delete')")
    public CommonResult<Boolean> deleteFundDetails(@RequestParam("id") BigInteger id) {
        fundDetailsService.deleteFundDetails(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户资金收支明细")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details:query')")
    public CommonResult<PayFundDetailsRespVO> getFundDetails(@RequestParam("id") BigInteger id) {
        PayFundDetailsDO fundDetails = fundDetailsService.getFundDetails(id);
        return success(PayFundDetailsConvert.INSTANCE.convert(fundDetails));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户资金收支明细列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:fund-details:query')")
    public CommonResult<List<PayFundDetailsRespVO>> getFundDetailsList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<PayFundDetailsDO> list = fundDetailsService.getFundDetailsList(ids);
        return success(PayFundDetailsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户资金收支明细分页")
    @PreAuthorize("@ss.hasPermission('pay:fund-details:query')")
    public CommonResult<PageResult<PayFundDetailsRespVO>> getFundDetailsPage(@Valid PayFundDetailsPageReqVO pageVO) {
        PageResult<PayFundDetailsDO> pageResult = fundDetailsService.getFundDetailsPage(pageVO);
        return success(PayFundDetailsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户资金收支明细 Excel")
    @PreAuthorize("@ss.hasPermission('pay:fund-details:export')")
    @OperateLog(type = EXPORT)
    public void exportFundDetailsExcel(@Valid PayFundDetailsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayFundDetailsDO> list = fundDetailsService.getFundDetailsList(exportReqVO);
        // 导出 Excel
        List<PayFundDetailsExcelVO> datas = PayFundDetailsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户资金收支明细.xls", "数据", PayFundDetailsExcelVO.class, datas);
    }

}
